<template>
  <a-modal
    title="查看工序"
    width="80%"
    :visible="visible"
    :confirmLoading="confirmLoading"
    :footer="null"
    @ok="handleSubmit"
    @cancel="handleCancel"
  >
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">
        <a-form-item
          label="工序名称"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
        >
          <a-input readOnly placeholder="请输入工序名称" v-decorator="['name', {rules: [{required: true, message: '请输入工序名称！'}]}]" />
        </a-form-item>
        <a-form-item
          label="工序编号"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"

        >
          <a-input readOnly placeholder="请输入工序编号" v-decorator="['code', {rules: [{required: true, message: '请输入工序编号！'}]}]" />
        </a-form-item>
        <a-form-item v-show="false"><a-input v-decorator="['id']" /></a-form-item>
        <a-form-item
          label="不良品项"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
        >
          <a-select :open="false" mode="multiple" style="width: 100%" placeholder="请选择不良品" v-decorator="['mulBadItem',{rules: [{ required: true, message: '请选择不良品' }]}]">
            <a-select-option disabled="true" v-for="(item,index) in mulBadItemData" :key="index" :value="item.code">{{ item.name }}</a-select-option>
          </a-select>
        </a-form-item>
        <a-form-item
          label="报工权限"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
        >
          <user-select
            :disabled="true"
            mode="multiple"
            placeholder="请选择报工权限"
            v-decorator="['reportRight', { rules: [ {required: false, message: '请选择报工权限！' }]}]"/>
        </a-form-item>
        <a-form-item
          label="备注"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"

        >
          <a-textarea readOnly placeholder="请输入备注" v-decorator="['remarks']" :auto-size="{ minRows: 3, maxRows: 6 }"/>
        </a-form-item>
      </a-form>
    </a-spin>

  </a-modal>
</template>

<script>
import { workStepEdit } from '@/api/modular/main/workstep/workStepManage'
import UserSelect from '@/components/MyComp/UserSelect'
export default {
  components: {
    UserSelect
  },
  data () {
    return {
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 }
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 15 }
      },
      visible: false,
      confirmLoading: false,
      mulBadItemData: [],
      form: this.$form.createForm(this)
    }
  },
  methods: {
    // 初始化方法
    edit (record) {
      this.visible = true
      const mulBadItem = this.$options
      this.mulBadItemData = mulBadItem.filters['dictData']('bad_item')
      setTimeout(() => {
        this.form.setFieldsValue(
          {
            code: record.code,
            id: record.id,
            mulBadItem: record.mulBadItem ? record.mulBadItem.split(';') : [],
            name: record.name,
            remarks: record.remarks,
            reportRight: record.reportRight ? record.reportRight.split(';') : []
          }
        )
      }, 100)
    },
    handleSubmit () {
      const { form: { validateFields } } = this
      this.confirmLoading = true
      validateFields((errors, values) => {
        if (!errors) {
          values.mulBadItem = values.mulBadItem.join(';')
          for (const key in values) {
            if (typeof (values[key]) === 'object' && values[key] != null) {
              values[key] = JSON.stringify(values[key])
            }
          }
          workStepEdit(values).then((res) => {
            if (res.success) {
              this.$message.success('编辑成功')
              this.confirmLoading = false
              this.$emit('ok', values)
              this.handleCancel()
            } else {
              this.$message.error(res.message)//  + res.message
            }
          }).finally((res) => {
            this.confirmLoading = false
          })
        } else {
          this.confirmLoading = false
        }
      })
    },
    handleCancel () {
      this.form.resetFields()
      this.visible = false
    }
  }
}
</script >
